## Script (Python) "assessmentexplorer_userchooser"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##title=Assessment Explorer (by User)
##parameters=

#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         assessmentexplorer_userchooser.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  A python script to control/handle requests from the
#               assessmentexplorer_userchooser_render.pt page
#
#####################################################################

#set the default batch size (number of users to display at once)
batchSize = 15

#get the request from the context... we will use it to find stuff!
request=context.REQUEST

#get the sercurity, user provider, assessment storage and credit management services
securityService = context.knowledgeengine_securityservice
userProviderService = context.knowledgeengine_userproviderservice
assessmentStorageService = context.knowledgeengine_assessmentstorageservice
creditManagementService = context.knowledgeengine_creditmanagementservice

#get the knowledge engine user
user = userProviderService.getUser(request["AUTHENTICATED_USER"].getId())

#get the users via a search if a search string was provided
#or get users from the user provider (and include the anonymous user)
if request.form.has_key('searchString') and request.get('searchString','') != '':
    users = userProviderService.searchUsers(request.get('searchString'))
else: 
    users = userProviderService.getUsers(includeAnonymousUser = 1)
    
nrUsers = len(users)

#determine the current batch
if request.form.has_key('batchNr'):
    batchNr = int(request.form['batchNr'])    
else:
    batchNr = 0

#determine the sort mode (IDENTITY, FULLNAME, EMAIL)
if request.form.has_key('sortBy'):
    sortBy = request.form['sortBy']
else:
    sortBy = 'IDENTITY'

#do we need to change the sort order?
if request.form.has_key('btnExplore'):
    sortBy = request.form['newSortBy']
    batchNr = 0

#sort the users
if sortBy == 'IDENTITY':
    users.sort(lambda x, y : -(x.getIdentity() < y.getIdentity()))
    
elif sortBy == 'FULLNAME':
    users.sort(lambda x, y : -(x.getFullname() < y.getFullname()))

elif sortBy == 'EMAIL':
    users.sort(lambda x, y : -(x.getEmail() < y.getEmail()))

#determine the number of batches
nrBatches = int(nrUsers / batchSize)
if nrBatches * batchSize < nrUsers:
    nrBatches = nrBatches + 1

#process navigation
if request.form.has_key('btnFirst'):
    batchNr = 0
elif request.form.has_key('btnBack'):
    batchNr = batchNr - 1
elif request.form.has_key('btnNext'):
    batchNr = batchNr + 1
elif request.form.has_key('btnLast'):
    batchNr = nrBatches + 1
elif request["REQUEST_METHOD"] == "POST" and not request.form.has_key('searchStringBtn'):
    #direct page navigation
    if request.has_key('page'):
        batchNr = int(request.form['page']) - 1
    else:
        batchNr = 0

#ensure we are still in the boundaries of the available batches
if batchNr < 0:
    batchNr = 0
elif batchNr > nrBatches:
    batchNr = nrBatches - 1

#trim the users collection to be the appropriate batch
users = users[batchNr * batchSize:batchNr * batchSize + batchSize]

#set the report information in the request
request.set('users', users)
request.set('sortBy', sortBy)
request.set('batchNr', batchNr)
request.set('nrBatches', nrBatches)
request.set('nrUsers', nrUsers)

#forward to the assessmentexplorer_userchooser_render.pt page
return context.assessmentexplorer_userchooser_render(context, request)